package com.woniu.aimd.controller;
import com.woniu.aimd.pojo.entity.SimplifyResponse;
import com.woniu.aimd.util.ResultObj;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("aiEssay")
public class SimplifyEssayController {
    private static final Logger logger = LoggerFactory.getLogger(answerQuestionController.class);

    @Resource
    private ChatClient deepSeekChatClientYMH;
    @PostMapping("generateSimplify")
    public ResultObj<String> generateSimplify(
            @RequestBody Map<String, String> body) {
        String essay = body.get("essay");
        if (essay == null || essay.isBlank()) {
            return ResultObj.error(400, "题目类型不能为空");
        }

        try {
            String Simplifyessay = Simplify(essay);
            return ResultObj.success(Simplifyessay);

        } catch (Exception e) {
            logger.error("生成题目失败: {}", e.getMessage(), e);
            return ResultObj.error(500, "生成题目失败，请稍后再试");
        }

    }
    private String Simplify(String essay) {
        String promptText = String.format("""
            文章是【%s】 ，我的要求是你把这个文章简化成不多于30个字的概述，概述用途是去准确的生成关于这个文章的练习题，所以概述需要准确的描述文章具体说什么
            以及所以的细节要点
            """,essay);
        logger.info("Prompt sent to AI: {}", promptText);

        SimplifyResponse response =deepSeekChatClientYMH.prompt()
                .user(u -> u.text(promptText))
                .call()
                .entity(SimplifyResponse.class);
        return response.getResult();
    }
}
